package com.boot.statemachine.config;

import com.boot.statemachine.states.Events;
import com.boot.statemachine.states.States;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

/**
 * @author canger
 * @Deseription 状态机的配置类
 * @create 2018/9/4 16:31
 **/
@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {

        private Logger logger= Logger.getLogger(getClass());

        @Override
        public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
                //定义状态机中的状态
                states
                        .withStates()
                        //初始化初始状态为UNPAID
                        .initial(States.UNPAID)
                        //则指定了使用上一步中定义的所有状态作为该状态机的状态定义。
                        .states(EnumSet.allOf(States.class));

        }
        @Override
        public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
                transitions
                        .withExternal()
                        //来源状态source    目标状态target
                                .source(States.UNPAID).target(States.WAITING_FOR_RECEIVE)
                        //出发指定事件
                                .event(Events.PAY)
                                .and()
                        .withExternal()
                                .source(States.WAITING_FOR_RECEIVE).target(States.DONE)
                                .event(Events.RECEIVE);
        }


//        @Override
//        public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
//                config
//                        .withConfiguration()
//                        // 指定状态机的处理监听器
//                        .listener(listener());
//        }
//        @Bean
//   public StateMachineListener<States, Events> listener(){
//
//        return new StateMachineListenerAdapter<States, Events>(){
//
//                @Override
//                public void transition(Transition<States, Events> transition) {
//                    if (transition.getTarget().getId()==States.UNPAID){
//                            logger.info("订单创建，待支付");
//                            return;
//                    }
//                    if (transition.getSource().getId()==States.UNPAID
//                        &&transition.getTarget().getId()==States.WAITING_FOR_RECEIVE){
//                        logger.info("用户完成支付，待收货");
//                        return;
//                    }
//                    if (transition.getSource().getId()==States.WAITING_FOR_RECEIVE
//                            &&transition.getTarget().getId()==States.DONE){
//                            logger.info("用户已收货，订单完成");
//                            return;
//                    }
//                }
//        };
//   }
}
